export const grid_mixin = {
  data() {
    return {
      grid: {
        sortable: false,//列表排序
        page: 1,
        size: 15,
        total: 0,
        pageSizes: [15, 30, 50, 100],
        rows: [], //加载的数据
        multiple: [],//table中多选对象
      }
    }
  },
  methods: {
    /**页序号调整*/
    pageNumChange(val) {
      this.grid.page = val
      this.gridLoadRefresh()
    },
    /**页容量调整*/
    pageSizeChange(val) {
      this.pageReset()
      this.grid.size = val
      this.gridLoadRefresh()
    },
    /**重置分页*/
    pageReset() {
      this.grid.page = 1
      this.grid.total = 0
      this.grid.size = 15
    },

    /**重置表格*/
    gridReset() {
      this.pageReset()
      this.grid.rows = []
      this.grid.multiple = []
    },


    /**创建实体*/
    entityCreate(data) {
      this.formMode(true, "添加", true, {...data, validStatus: 1})
    },
    /**编辑实体*/
    entryModify(entry) {
      this.formMode(true, "编辑", true, entry)
    },
    /**详情实体*/
    entryDetail(entry) {
      this.formMode(true, "详情", false, entry)
    },

    /**单条删除*/
    entityDelete(entry) {
      this.entityDelete(entry.id)
    },
    /**批量删除*/
    entryBatchDelete() {
      if (this.grid.multiple && this.grid.multiple.length > 0) {
        this.entityDelete(this.grid.multiple.map(m => m.id))
      } else {
        this.$message.warning('请先选择待删除的记录！')
      }
    },
    /**记录表格选中变化*/
    gridMultipleChange(chooseRows) {
      this.grid.multiple = chooseRows
    },
    /**表格排序变化*/
    gridSortChange(sortable) {
      this.grid.page = 1
      this.search.order = sortable.prop + " " + (sortable.order == 'descending' ? 'DESC' : 'ASC')
      this.gridLoadRefresh()
    },
    /**三种刷新方式*/
    refreshCommand(command) {
      switch (command) {
        case 'keepStateRefresh':
          this.keepStateRefresh()
          break
        case 'specifySearch':
          this.specifySearch()
          break
        case 'initializeRefresh':
          this.initializeRefresh()
          break
        default:
          this.initializeRefresh()
          break
      }
    },
    /**重置分页刷新*/
    specifySearch() {
      this.grid.page = 1
      this.gridLoadRefresh()
    },

    /**保持状态刷新*/
    keepStateRefresh() {
      this.gridLoadRefresh()
    },
    /**初始化刷新*/
    initializeRefresh() {
      this.gridReset()
      this.formReset()
      this.searchReset()
      this.gridLoadRefresh()
    },
  }
}